﻿using System.Collections.Generic;
using UnityEditor;

namespace UnityEngine.UI.Extensions.zLeon
{
    [CanEditMultipleObjects]
    [CustomEditor(typeof(AlphabetAndNumberTextPic))]
    public class AlphabetAndNumberTextPicEditor : UnityEditor.UI.TextEditor
    {

        private SerializedProperty ImageScalingFactorProp;
        private SerializedProperty hyperlinkColorProp;
        private SerializedProperty imageOffsetProp;
        [Tooltip("A-Z")]
        private SerializedProperty AlphabetList;
        [Tooltip("0-9-.")]
        private SerializedProperty NumberList;
        [Tooltip("这是Icon List")]
        private SerializedProperty SetIconList;
        private SerializedProperty iconList;


        protected override void OnEnable()
        {
            base.OnEnable();
            ImageScalingFactorProp = serializedObject.FindProperty("ImageScalingFactor");
            AlphabetList = serializedObject.FindProperty("AlphabetList");
            NumberList = serializedObject.FindProperty("NumberList");
            hyperlinkColorProp = serializedObject.FindProperty("hyperlinkColor");
            imageOffsetProp = serializedObject.FindProperty("imageOffset");
            iconList = serializedObject.FindProperty("inspectorIconList");
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            serializedObject.Update();
            EditorGUILayout.PropertyField(imageOffsetProp, new GUIContent("Image Offset"));
            EditorGUILayout.PropertyField(ImageScalingFactorProp, new GUIContent("Image Scaling Factor"));
            EditorGUILayout.PropertyField(hyperlinkColorProp, new GUIContent("Hyperlink Color"));
            EditorGUILayout.PropertyField(AlphabetList, new GUIContent("Alphabet List"), true);
            EditorGUILayout.PropertyField(NumberList, new GUIContent("Number List"), true);
            EditorGUILayout.PropertyField(iconList, new GUIContent("Icon List"), true);
            if (GUILayout.Button("SetIconList"))
            {
                SetIconListData();
            }

            serializedObject.ApplyModifiedProperties();
        }

        public void SetIconListData()
        {

            AlphabetAndNumberTextPic data = target as AlphabetAndNumberTextPic;
            data.inspectorIconList = null;

            List<TextPic.IconName> AlphabetArr = new List<TextPic.IconName>();
            for (char i = 'A'; i <= 'Z'; i++)
            {
                AlphabetArr.Add(new TextPic.IconName() { name = i.ToString(), sprite = null, offset = new Vector2(0, 0), scale = new Vector2(1, 1) });
            }

            for (int i = 0; i < 26; i++)
            {
                AlphabetArr[i].sprite = data.AlphabetList[i];
            }

            for (int i = 0; i < 10; i++)
            {
                AlphabetArr.Add(new TextPic.IconName() { name = i.ToString(), sprite = data.NumberList[i], offset = new Vector2(0, 0), scale = new Vector2(1, 1) });
            }

            AlphabetArr.Add(new TextPic.IconName() { name = ".", sprite = data.NumberList[10], offset = new Vector2(0, 0), scale = new Vector2(1, 1) });

            data.inspectorIconList = AlphabetArr.ToArray();
        }
    }
}
